﻿using System;
using System.Linq;
using System.Linq.Expressions;
using System.Linq.Dynamic;using System.Text;
using System.Data.Linq;
using System.Transactions;
using LinqToSqlExtensions;using System.ServiceModel;
using System.Collections;
using System.Collections.Generic;
using CardsServer.DAL;

namespace CardsServer.WCF
{
    public partial interface IService
    {
        [OperationContract]
        string shop_Add(shop model, int ACID);


        [OperationContract]
        string shop_Update(shop model, int ACID);


        [OperationContract]
        string shopByshopcode_Delete(string shopcode, int ACID);


        [OperationContract]
        shop shopByshopcode_Get(string shopcode, int ACID);


        [OperationContract]
        List<shop> shop_Get(string sortBy, int ACID);


        [OperationContract]
        List<View_shop_lite> View_shop_lite_Get(int ACID);

    }
    public partial class Service : IService
    {
        #region  添加shop表数据
        /// <summary>
        /// 添加shop
        /// </summary>
        /// <param name="model">shop实体</param>
        /// <returns>空或错误</returns>
        public string shop_Add(shop model, int ACID)
        {
            if (string.IsNullOrEmpty(model.shopcode))
            {
                return "『分支机构编号』不能为空！";
            }
            if (string.IsNullOrEmpty(model.name))
            {
                return "『分支机构名称』不能为空！";
            }
            using (CardsServer.DAL.DataClassesDataContext ctx = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
            {
                try
                {
                    if (ctx.shop.Count(p => p.shopcode == model.shopcode) > 0)
                    {
                        return "主键已存在！";
                    }
                    if (ctx.shop.Count(p => p.name == model.name) > 0)
                    {
                        return "分支机构名称已存在！";
                    }
                    ctx.shop.InsertOnSubmit(model);
                    ctx.SubmitChanges();
                    return string.Empty;
                }
                catch (Exception ex)
                {
                    return " 添加失败!错误详细信息：" + ex.Message;
                }
            }
        }
        #endregion

        #region  添加shop表数据事物用重构
        /// <summary>
        /// 添加shop事物用重构
        /// </summary>
        /// <param name="model">shop实体</param>
        /// <returns>空或错误</returns>
        private string shop_Add(shop model, int ACID, CardsServer.DAL.DataClassesDataContext ctx)
        {
            if (string.IsNullOrEmpty(model.shopcode))
            {
                return "『分支机构编号』不能为空！";
            }
            if (string.IsNullOrEmpty(model.name))
            {
                return "『分支机构名称』不能为空！";
            }
            if (ctx.shop.Count(p => p.shopcode == model.shopcode) > 0)
            {
                return "主键已存在！";
            }
            if (ctx.shop.Count(p => p.name == model.name) > 0)
            {
                return "分支机构名称已存在！";
            }
            ctx.shop.InsertOnSubmit(model);
            return string.Empty;
        }
        #endregion



        #region  修改shop表数据
        /// <summary>
        /// 修改shop
        /// </summary>
        /// <param name="model">shop实体</param>
        /// <returns>空或错误</returns>
        public string shop_Update(shop model, int ACID)
        {
            if (string.IsNullOrEmpty(model.shopcode))
            {
                return "『分支机构编号』不能为空！";
            }
            if (string.IsNullOrEmpty(model.name))
            {
                return "『分支机构名称』不能为空！";
            }
            using (CardsServer.DAL.DataClassesDataContext ctx = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
            {
                try
                {
                    if (ctx.shop.Count(p => p.shopcode == model.shopcode) != 1)
                    {
                        return "未找到指定记录或记录不唯一！";
                    }
                    shop shopmodel = ctx.shop.Single(p => p.shopcode == model.shopcode);
                        shopmodel.shopcode = model.shopcode;
                        shopmodel.name = model.name;
                        shopmodel.code = model.code;
                        shopmodel.legalrep = model.legalrep;
                        shopmodel.bank = model.bank;
                        shopmodel.bankaccno = model.bankaccno;
                        shopmodel.taxno = model.taxno;
                        shopmodel.address = model.address;
                        shopmodel.phone = model.phone;
                        shopmodel.remarks = model.remarks;
                    ctx.SubmitChanges();
                    return string.Empty;
                }
                catch (Exception ex)
                {
                    return " 修改失败!错误详细信息：" + ex.Message;
                }
            }
        }
        #endregion



        #region  删除shop表Byshopcode数据
        /// <summary>
        /// 删除shopByshopcode
        /// </summary>
        /// <param name="shopcode">分支机构编号</param>
        /// <returns>空或错误</returns>
        public string shopByshopcode_Delete(string shopcode, int ACID)
        {
            if (string.IsNullOrEmpty(shopcode))
            {
                return "『分支机构编号』不能为空！";
            }
            using (CardsServer.DAL.DataClassesDataContext ctx = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
            {
                try
                {
                    if (ctx.shop.Count(p => p.shopcode == shopcode) != 1)
                    {
                        return null;
                    }
                    shop model = ctx.shop.Single(p => p.shopcode == shopcode);
                    ctx.shop.DeleteOnSubmit(model);
                    ctx.SubmitChanges();
                    return string.Empty;
                }
                catch (Exception ex)
                {
                    return " 删除失败!错误详细信息：" + ex.Message;
                }
            }
        }
        #endregion

        #region  删除shop表Byshopcode数据[事物用重构]
        /// <summary>
        /// 删除shopByshopcode[事物用重构]
        /// </summary>
        /// <param name="shopcode">分支机构编号</param>
        /// <returns>空或错误</returns>
        private string shopByshopcode_Delete(string shopcode, int ACID, CardsServer.DAL.DataClassesDataContext ctx)
        {
            if (string.IsNullOrEmpty(shopcode))
            {
                return "『分支机构编号』不能为空！";
            }
            if (ctx.shop.Count(p => p.shopcode == shopcode) != 1)
            {
                return null;
            }
            shop model = ctx.shop.Single(p => p.shopcode == shopcode);
            ctx.shop.DeleteOnSubmit(model);
            return string.Empty;
        }
        #endregion



        #region  获取shop表Byshopcode数据
        /// <summary>
        /// 获取shopByshopcode
        /// </summary>
        /// <param name="shopcode">分支机构编号</param>
        /// <returns>当前表</returns>
        public shop shopByshopcode_Get(string shopcode, int ACID)
        {
            if (string.IsNullOrEmpty(shopcode))
            {
                return null;
            }
            using (CardsServer.DAL.DataClassesDataContext ctx = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
            {
                try
                {
                    if (ctx.shop.Count(p => p.shopcode == shopcode) != 1)
                    {
                        return null;
                    }
                    return ctx.shop.SingleOrDefault(p => p.shopcode == shopcode);
                }
                catch (Exception ex)
                {
                    return null;
                }
            }
        }
        #endregion



        #region  获取shop表数据
        /// <summary>
        /// 获取shop
        /// </summary>
        /// <param name="sortBy">排序属性名</param>
        /// <param name="sortType">排序类型0=升序 1=降序</param>
        /// <returns>List<shop></returns>
        public List<shop> shop_Get(Expression<Func<shop, bool>> predicate, string sortBy, int ACID)
        {
            using (CardsServer.DAL.DataClassesDataContext ctx = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
            {
                try
                {
                    if (predicate == null)
                    {
                        if (string.IsNullOrWhiteSpace(sortBy))
                        {
                            return ctx.shop.ToList();
                        }
                        else
                        {
                            return ctx.shop.OrderBy(sortBy).ToList();
                        }
                    }
                    else
                    {
                        if (string.IsNullOrWhiteSpace(sortBy))
                        {
                            return ctx.shop.Where(predicate).ToList();
                        }
                        else
                        {
                            return ctx.shop.Where(predicate).OrderBy(sortBy).ToList();
                        }
                    }
                }
                catch (Exception ex)
                {
                    return null;
                }
            }
        }
        #endregion


        #region  获取shop表数据
        /// <summary>
        /// 获取shop
        /// </summary>
        /// <param name="sortBy">排序属性名</param>
        /// <param name="sortType">排序类型0=升序 1=降序</param>
        /// <returns>List<shop></returns>
        public List<shop> shop_Get(string sortBy, int ACID)
        {
            using (CardsServer.DAL.DataClassesDataContext ctx = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
            {
                try
                {
                    return shop_Get(null, sortBy, ACID);
                }
                catch (Exception)
                {
                    return null;
                }
            }
        }
        #endregion


        #region  获取shop表数据计数
        /// <summary>
        /// 获取shop表数据计数
        /// </summary>
        /// <returns>List<shop></returns>
        public int shop_GetCount(Expression<Func<shop, bool>> predicate, int ACID)
        {
            using (CardsServer.DAL.DataClassesDataContext ctx = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
            {
                try
                {
                    if (predicate == null)
                    {
                        return ctx.shop.Count();

                    }
                    else
                    {
                        return ctx.shop.Where(predicate).Count();
                    }
                }
                catch
                {
                    return 0;
                }
            }
        }
        #endregion




        #region  获取shop分页表数据
        /// <summary>
        /// 获取shop分页表数据
        /// </summary>
        /// <param name="pageIndex">当前页</param>
        /// <param name="pageSize">页大小</param>
        /// <param name="sortBy">排序属性名</param>
        /// <param name="sortType">排序类型0=升序 1=降序</param>
        /// <returns>List<shop></returns>
        public List<shop> shop_Getpage(int pageIndex, int pageSize, Expression<Func<shop, bool>> predicate, string sortBy, int ACID)
        {
            using (CardsServer.DAL.DataClassesDataContext ctx = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
            {
                try
                {
                    if (predicate == null)
                    {
                        if (string.IsNullOrWhiteSpace(sortBy))
                        {
                            return ctx.shop.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
                        }
                        else
                        {
                            return ctx.shop.OrderBy(sortBy).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
                        }
                    }
                    else
                    {
                        if (string.IsNullOrWhiteSpace(sortBy))
                        {
                            return ctx.shop.Where(predicate).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
                        }
                        else
                        {
                            return ctx.shop.Where(predicate).OrderBy(sortBy).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
                        }
                    }
                }
                catch (Exception ex)
                {
                    return null;
                }
            }
        }
        #endregion


        #region  批量修改shop表数据
        /// <summary>
        /// 批量修改shop
        /// </summary>
        /// <param name="predicate">Expression<Func<shop, bool>></param> 查找表达式树
        /// <param name="updater">Expression<Func<shop,shop>> 更新表达式树</param>
        /// <param name="returns">影响记录数</param>
        public int shop_batchUpdate(Expression<Func<shop, bool>> predicate, Expression<Func<shop, shop>> updater, int ACID)
        {
            using (TransactionScope ts = new TransactionScope())
            {
                using (CardsServer.DAL.DataClassesDataContext ctx = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
                {
                    try
                    {
                        int ok = ctx.shop.Update(predicate, updater);
                        ts.Complete();
                        return ok;
                    }
                    catch (Exception ex)
                    {
                        return 0;
                    }
                }
            }
        }
        #endregion


        #region  批量修改shop表数据事物用重构
        /// <summary>
        /// 批量修改shop事物用重构
        /// </summary>
        /// <param name="predicate">Expression<Func<shop, bool>></param> 查找表达式树
        /// <param name="updater">Expression<Func<shop,shop>> 更新表达式树</param>
        /// <param name="ctx">数据上下文</param>
        /// <param name="returns">影响记录数</param>
        private int shop_batchUpdate(Expression<Func<shop, bool>> predicate, Expression<Func<shop, shop>> updater, int ACID, CardsServer.DAL.DataClassesDataContext ctx)
        {
            int ok = ctx.shop.Update(predicate, updater);
            return ok;
        }
        #endregion


        #region  批量删除shop表数据
        /// <summary>
        /// 批量删除shop
        /// </summary>
        /// <param name="predicate">Expression<Func<shop, bool>></param> 查找表达式树
        /// <param name="returns">影响记录数</param>
        public int shop_batchDel(Expression<Func<shop, bool>> predicate, int ACID)
        {
            using (TransactionScope ts = new TransactionScope())
            {
                using (CardsServer.DAL.DataClassesDataContext ctx = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
                {
                    try
                    {
                        int ok = ctx.shop.Delete(predicate);
                        ts.Complete();
                        return ok;
                    }
                    catch (Exception ex)
                    {
                        return 0;
                    }
                }
            }
        }
        #endregion


        #region  批量删除shop表数据事物用重构
        /// <summary>
        /// 批量删除shop事物用重构
        /// </summary>
        /// <param name="predicate">Expression<Func<shop, bool>></param> 查找表达式树
        /// <param name="ctx">数据上下文</param>
        /// <param name="returns">影响记录数</param>
        private int shop_batchDel(Expression<Func<shop, bool>> predicate, int ACID, CardsServer.DAL.DataClassesDataContext ctx)
        {
            int ok = ctx.shop.Delete(predicate);
            return ok;
        }
        #endregion

        public List<View_shop_lite> View_shop_lite_Get(int ACID)
        {
            using (CardsServer.DAL.DataClassesDataContext ctx = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
            {
                return ctx.View_shop_lite.ToList();
            }
        }
    }
}

